using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy1 : Enemy
{
    void Start()
    {
        anim = GetComponent<Animator>();
        speed = speed * -1;
        anim = GetComponent<Animator>();
    }
    void Update()
    {
        //Debug.Log(transform.right);
        transform.position += -transform.right * speed * Time.deltaTime;
    }

    private void FixedUpdate()
    {
        Vector2 startPos = transform.position + transform.right;
        Vector2 HitPos = transform.position + (transform.right * 1.2f);
        Debug.DrawLine(startPos, HitPos, Color.red);
        Debug.DrawLine(startPos, startPos + (Vector2.down*2.5f), Color.red);
        //Debug.DrawLine(startPos, startPos + (Vector2.down * 2.5f), Color.red);
        RaycastHit2D hit1 = Physics2D.Raycast(startPos, Vector2.down, 2.5f, LayerMask.GetMask("Floor"));
        RaycastHit2D hit2 = Physics2D.Raycast(startPos, transform.right, 1.2f, LayerMask.GetMask("Player"));
        RaycastHit2D hit3 = Physics2D.Raycast(startPos, transform.right, 1.2f, LayerMask.GetMask("Floor"));
        if (!hit1 || hit3)
        {
            transform.right = -transform.right;
        }
        if(hit2)
        {
            anim.SetTrigger("Attck");
            StartCoroutine(attck());
        }
    }

    IEnumerator attck()
    {
        yield return new WaitForSeconds(0.5f);
        Vector2 startPos = transform.position + transform.right;
        RaycastHit2D hit = Physics2D.Raycast(startPos, transform.right, 1.2f, LayerMask.GetMask("Player"));
        if (hit) GameObject.Find("Player").GetComponent<Player>().behurt();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("PlayerBullet"))
        {
            anim.SetTrigger("BeHurt"); 
            Debug.Log(HP);
            if (HP <= 0)
            {
                getGold(100);
                gameObject.layer = LayerMask.NameToLayer("BG");
                anim.SetTrigger("Die");
            }
        }
    }

}
